package com.shankungfu.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.shankungfu.R;
import com.shankungfu.bean.MyInterestDataBean;
import com.shankungfu.main.PickItemDetailActivity;
import com.shankungfu.main.UserCenterActivity;

public class UserListlAdapter extends BaseAdapter {

	private Context context;
	private List<MyInterestDataBean> list;
	private DisplayImageOptions options;
	private ImageLoadingListener animateFirstListener = new AnimateFirstDisplayListener();
	private ImageLoader imageLoader;

	public UserListlAdapter(Context context, DisplayImageOptions options) {
		this.context = context;
		this.options = options;
		imageLoader = ImageLoader.getInstance();
		this.list = new ArrayList<MyInterestDataBean>();
	}
	
	public void setData(List<MyInterestDataBean> list) {
		if(list == null){
			return;
		}
		this.list = list;
		notifyDataSetChanged();
	}
	public void addData(List<MyInterestDataBean> list) {
		if(list == null){
			return;
		}
		this.list.addAll(list);
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
		return list != null ? list.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = View.inflate(context, R.layout.user_center_list_layout,
					null);
			holder.pic = (ImageView) convertView.findViewById(R.id.image);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.detail = (TextView) convertView.findViewById(R.id.detail);

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		final MyInterestDataBean  dataBean = list.get(position);
		
		holder.title.setText(dataBean.getMy_lead_title());
		holder.detail.setText(dataBean.getMy_lead_txt());
		
		imageLoader.displayImage(dataBean.getMy_lead_img_url(), holder.pic, options, animateFirstListener);
		
		
		convertView.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent(context, PickItemDetailActivity.class);
				intent.putExtra(PickItemDetailActivity.PROJECT_ID,
						dataBean.getMy_lead_Id());
				context.startActivity(intent);
			}
		});

		return convertView;
	}

	private class ViewHolder {
		private ImageView pic;
		private TextView title;
		private TextView detail;
	}
	
	private static class AnimateFirstDisplayListener extends SimpleImageLoadingListener {

		static final List<String> displayedImages = Collections.synchronizedList(new LinkedList<String>());

		@Override
		public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
			if (loadedImage != null) {
				ImageView imageView = (ImageView) view;
				boolean firstDisplay = !displayedImages.contains(imageUri);
				if (firstDisplay) {
					FadeInBitmapDisplayer.animate(imageView, 500);
					displayedImages.add(imageUri);
				}
			}
		}
	}
}
